package com.ebelter.bodyfatscale.ui.pager.findpsw;


import android.app.Activity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.ebelter.bodyfatscale.R;
import com.ebelter.bodyfatscale.common.Constants;
import com.ebelter.bodyfatscale.moudules.https.NetUtils;
import com.ebelter.bodyfatscale.moudules.https.interfaces.HttpResponse;
import com.ebelter.bodyfatscale.moudules.https.response.getEmailValidateCodeResponse;
import com.ebelter.bodyfatscale.ui.view.CountDownTextView;
import com.ebelter.bodyfatscale.util.ToastUtil;
import com.ebelter.bodyfatscale.util.UserSpUtil;
import com.ebelter.bodyfatscale.util.VerifyUtils;
import com.ebelter.bodyfatscale.util.ViewUtils;

import butterknife.BindView;

public class FindPswPage_Email extends BasePagerFindPsw {


    @BindView(R.id.et_email_findemail)
    EditText et_email_findemail;

    @BindView(R.id.et_emailverificationcode_findemail)
    EditText et_emailverificationcode_findemail;

    @BindView(R.id.page_register_countDownTv)
    CountDownTextView findemail_countDownTv;



    public FindPswPage_Email(Activity aty) {
        super(aty);
    }

    @Override
    protected int loadLayoutById() {
        return R.layout.findpsw_email;
    }

    @Override
    public void initViews() {
        findemail_countDownTv.startWord();
        findemail_countDownTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendEmailVC();
            }
        });
        String email=UserSpUtil.readString(Constants.IUser.EMAIL);
        if (!TextUtils.isEmpty(email)){
            et_email_findemail.setText(email);
        }
    }

    //发送邮箱验证码
    private void sendEmailVC() {
        String mail=et_email_findemail.getText().toString().trim();
        if (TextUtils.isEmpty(mail)){
            ToastUtil.show(R.string.Email_must_not_be_empty);
            return;
        }

        if (!VerifyUtils.emailFormat(mail)){
            ToastUtil.show(R.string.Incorrect_email_format);
            ViewUtils.shakeView(et_email_findemail);
            return;
        }
        if (!NetUtils.available()){
            ToastUtil.show(R.string.Network_error);
            return;
        }
        NetUtils.getInstance().getEmailValidateCodeRequest(getActivity(), mail, 2, new HttpResponse<getEmailValidateCodeResponse>() {
            @Override
            public void result(boolean isRequestSuccess, String requestDes, getEmailValidateCodeResponse getEmailValidateCodeResponse) {
                if (isRequestSuccess){
                    if (getEmailValidateCodeResponse.resultCode==0){
                        ToastUtil.show(R.string.The_email_has_been_sent);
                        findemail_countDownTv.click();
                        getActivity().setNextButton(true);
                    }else if (getEmailValidateCodeResponse.resultCode==104){//邮箱没有注册
                        ToastUtil.show(R.string.Email_is_not_registered);
                    }else if (getEmailValidateCodeResponse.resultCode==110){//服务器异常
                        ToastUtil.show(R.string.An_exception_occurred_on_the_server);
                    }else {
                        ToastUtil.show(R.string.Failed_to_send_mail);
                    }
                }else {
                    ToastUtil.show(R.string.Network_connection_error);
                }
            }
        });
    }




    @Override
    public void initData(Object data) {

    }

    public String getEmail(){
        return et_email_findemail.getText().toString().trim();
    }
    public void shake_email_et(){
        ViewUtils.shakeView(et_email_findemail);
    }
    public String getEmailCode(){
        return et_emailverificationcode_findemail.getText().toString().trim();
    }
    public void shake_emailCode_et(){
        ViewUtils.shakeView(et_emailverificationcode_findemail);
    }
}
